package com.spdg.ring.sqlite.dao.impl;

import java.util.List;

import android.content.Context;
import cn.wolf.sqlite.dao.impl.BaseDaoImpl;
import cn.wolf.tools.Log;

import com.spdg.ring.sqlite.DBHelper;
import com.spdg.ring.sqlite.dao.MenuDao;
import com.spdg.ring.sqlite.model.Menu;
/**
 * <p>Title: Menu Dao       </p>
 * <p>Description: 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-11             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class MenuDaoImpl extends BaseDaoImpl<Menu> implements MenuDao {
	private static MenuDaoImpl menuDao;

	private MenuDaoImpl(Context context) {
		super(new DBHelper(context));
	}

	public static MenuDao newInstance(Context context) {
		if (menuDao == null) {
			menuDao = new MenuDaoImpl(context);
		}

		return menuDao;
	}

	@Override
	public boolean isExist(Menu menu) {
		String sql = "select * from t_menu where file = ?";
		return menuDao.isExist(sql, new String[] { menu.getFile() });
	}

	@Override
	public void delete(Menu menu) {
		String sql = "delete from t_menu where file = ?";

		menuDao.execSql(sql, new String[] { menu.getFile() });
	}

	@Override
	public void deleteAll() {
		menuDao.deleteAll();
	}
	
	@Override
	public void update(Menu entity) {
		if(entity == null) {
			Log.e("Menu object is null");
			return;
		}
		String key = entity.getFile();
		Menu menu = findByKey(key);
		if(menu != null) {
			entity.setId(menu.getId());
		}
		super.update(entity);
	}

	@Override
	public Menu findByKey(String key) {
		String sql = "select * from t_menu where file = ?";
		List<Menu> menuList = rawQuery(sql, new String[]{key});
		if(menuList != null && menuList.size() > 0) {
			return menuList.get(0);
		}
		return null;
	}
}
